home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / c / qtools0.2-src.lha / src / libqtools / wad.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  21.2 KB  |  780 lines

  1. #define    LIBQTOOLS_CORE
  2. #include "../include/libqtools.h"
  3. #include "../include/libqdisplay.h"
  4.  
  5. /*
  6.  * WAD2-tools
  7.  *
  8.  * entryName    == 0 -> OP_EXTRACT all
  9.  * destDir == 0 -> OP_EXTRACT to current directory
  10.  */
  11.  
  12. bool CheckWAD2(HANDLE wadFile, struct wadheader * Header, bool newWad)
  13. {
  14.   if (wadFile) {
  15.     __lseek(wadFile, 0, SEEK_END);
  16.     if (!__ltell(wadFile)) {
  17.       if (newWad) {
  18.     /* file is new */
  19.     Header->magic.integer = BigLong(MAGIC_WAD2);
  20.     Header->offset = LittleLong(sizeof(struct wadheader));
  21.  
  22.     Header->numentries = LittleLong(0);
  23.     __write(wadFile, Header, sizeof(struct wadheader));
  24.  
  25.     return TRUE;
  26.       }
  27.       else
  28.     return FALSE;
  29.     }
  30.     else {
  31.       __lseek(wadFile, 0, SEEK_SET);
  32.       __read(wadFile, Header, sizeof(struct wadheader));
  33.  
  34.       return Header->magic.integer == BigLong(MAGIC_WAD2) ? TRUE : FALSE;
  35.     }
  36.   }
  37.   else
  38.     return FALSE;
  39. }
  40.  
  41. /*
  42.  * findwad2 return ever one entry more than exists!
  43.  */
  44. struct wadentry *FindWAD2(HANDLE wadFile, char *entryName, struct wadheader *Header, struct wadentry **Entry, filetype wadType)
  45. {
  46.   struct wadentry *allEntries;
  47.  
  48.   __lseek(wadFile, LittleLong(Header->offset), SEEK_SET);
  49.   if ((*Entry = allEntries = (struct wadentry *)tmalloc((LittleLong(Header->numentries) + 1) * sizeof(struct wadentry)))) {
  50.     int i;
  51.  
  52.     __read(wadFile, allEntries, (LittleLong(Header->numentries)) * sizeof(struct wadentry));
  53.  
  54.     if (entryName) {
  55.       for (i = 0; i < LittleLong(Header->numentries); i++) {
  56.     if (!fnmatch(entryName, allEntries->name, FNM_PATHNAME))
  57.       /* if ((allEntries->type == wadType) || (wadType == TYPE_UNKNOWN)) */
  58.         /* return offset of valid entry */
  59.         return allEntries;
  60.     allEntries++;
  61.       }
  62.     }
  63.     /* not in wadFile */
  64.     return 0;
  65.   }
  66.   else
  67.     return (struct wadentry *)-1;
  68. }
  69.  
  70. struct wadentry *SearchWAD2(char *entryName, struct wadheader *Header, struct wadentry *allEntries, filetype wadType)
  71. {
  72.   int i;
  73.  
  74.   for (i = 0; i < LittleLong(Header->numentries); i++) {
  75.     if (!fnmatch(entryName, allEntries->name, FNM_PATHNAME))
  76.       /* if ((allEntries->type == wadType) || (wadType == TYPE_UNKNOWN)) */
  77.         return allEntries;
  78.     allEntries++;
  79.   }
  80.   return 0;
  81. }
  82.  
  83. struct palpic *GetWAD2Picture(HANDLE wadFile, struct wadentry *Entry)
  84. {
  85.   struct palpic *Picture = 0;
  86.  
  87.   __lseek(wadFile, LittleLong(Entry->offset), SEEK_SET);
  88.  
  89.   switch (Entry->type) {
  90.     case WAD2_PALETTE:
  91.       /*
  92.        * there MUST be only one palette in the wad, which is exactly the one we took above
  93.        */
  94.       if ((Picture = pmalloc(16, 16, 0, Entry->name))) {
  95.     short int i;
  96.  
  97.     for (i = 0; i < 256; i++)
  98.       Picture->rawdata[i] = (unsigned char)i;
  99.       }
  100.       break;
  101.     case WAD2_STATUSBAR:
  102.     case WAD2_CONPIC:
  103.       switch (Entry->compr) {
  104.     case CMP_NONE:
  105.     case CMP_MIP0:
  106.       Picture = GetLMP(wadFile, Entry->name);
  107.       break;
  108.     case CMP_LZ77:
  109.     case (CMP_LZ77 | CMP_MIP0):{
  110.         struct lump *Lump;
  111.  
  112.         if ((Lump = (struct lump *)GetLZ77(wadFile, LittleLong(Entry->wadsize)))) {
  113.           Picture = ParseLMP(Lump, Entry->name);
  114.           tfree(Lump);
  115.         }
  116.       }
  117.       break;
  118.     default:
  119.       break;
  120.       }
  121.       break;
  122.     case WAD2_MIPMAP:
  123.     case 0:
  124.       /*
  125.        * fix me!!! this is bogus 
  126.        */
  127.       switch (Entry->compr) {
  128.     case CMP_NONE:
  129.     case CMP_MIP0:
  130.       Picture = GetMipMap(wadFile, MIPMAP_0);
  131. #ifdef DEBUG
  132.       mprintf("get first 4 bytes %x%x%x%x (%x%x%x,%x%x%x,%x%x%x)\n", Picture->rawdata[0], Picture->rawdata[1], Picture->rawdata[2], Picture->rawdata[3],
  133.       Picture->palette[0].r, Picture->palette[0].g, Picture->palette[0].b,
  134.       Picture->palette[1].r, Picture->palette[1].g, Picture->palette[1].b,
  135.       Picture->palette[2].r, Picture->palette[2].g, Picture->palette[2].b);
  136. #endif
  137.       break;
  138.     case CMP_LZ77:
  139.     case (CMP_LZ77 | CMP_MIP0):{
  140.         struct mipmap *MipMap;
  141.  
  142.         if ((MipMap = (struct mipmap *)GetLZ77(wadFile, LittleLong(Entry->wadsize)))) {
  143.           Picture = ParseMipMap(MipMap, MIPMAP_0);
  144.           tfree(MipMap);
  145.         }
  146.       }
  147.       break;
  148.     default:
  149.       break;
  150.       }
  151.       break;
  152.     default:
  153.       break;
  154.   }
  155.   return Picture;
  156. }
  157.  
  158. struct rawdata *GetWAD2Raw(HANDLE wadFile, struct wadentry *Entry)
  159. {
  160.   struct rawdata *rawData = 0;
  161.  
  162.   __lseek(wadFile, LittleLong(Entry->offset), SEEK_SET);
  163.  
  164.   switch (Entry->compr) {
  165.     case CMP_NONE:
  166.       rawData = GetRaw(wadFile, Entry->name, LittleLong(Entry->wadsize));
  167.       break;
  168.     case CMP_MIP0:{
  169.     struct palpic *MipMap;
  170.  
  171.     if ((MipMap = GetMipMap(wadFile, MIPMAP_0))) {
  172.       if ((rawData = rmalloc(MIP_MULT(LittleLong(MipMap->width) * LittleLong(MipMap->height)) + sizeof(struct mipmap), Entry->name))) {
  173.         PasteMipMap((struct mipmap *)rawData->rawdata, MipMap);
  174.       }
  175.       pfree(MipMap);
  176.     }
  177.       }
  178.       break;
  179.     case CMP_LZ77:{
  180.     char *Data;
  181.  
  182.     if ((Data = GetLZ77(wadFile, LittleLong(Entry->wadsize)))) {
  183.       rawData = ParseRaw(Data, Entry->name, LittleLong(Entry->memsize));
  184.       tfree(Data);
  185.     }
  186.       }
  187.       break;
  188.     case (CMP_LZ77 | CMP_MIP0):{
  189.     struct mipmap *GetMip;
  190.     struct palpic *MipMap;
  191.  
  192.     if ((GetMip = (struct mipmap *)GetLZ77(wadFile, LittleLong(Entry->wadsize)))) {
  193.       if ((MipMap = ParseMipMap(GetMip, MIPMAP_0))) {
  194.         if ((rawData = rmalloc(MIP_MULT(LittleLong(MipMap->width) * LittleLong(MipMap->height)) + sizeof(struct mipmap), Entry->name))) {
  195.           PasteMipMap((struct mipmap *)rawData->rawdata, MipMap);
  196.         }
  197.         pfree(MipMap);
  198.       }
  199.       tfree(GetMip);
  200.     }
  201.       }
  202.       break;
  203.     default:
  204.       break;
  205.   }
  206.  
  207.   return rawData;
  208. }
  209.  
  210. bool ExtractWAD2(HANDLE wadFile, FILE * script, char *destDir, char *entryName, unsigned char outType, operation procOper, filetype wadType)
  211. {
  212.   struct wadheader Header;
  213.   struct wadentry *Entry, *allEntries;
  214.   struct rgb *oldCache = cachedPalette;
  215.   bool retval = FALSE;
  216.  
  217.   if (CheckWAD2(wadFile, &Header, FALSE)) {
  218.     /* we assume that the dir is at the end of the file!!!!! */
  219.     switch ((long int)(Entry = FindWAD2(wadFile, entryName, &Header, &allEntries, wadType))) {
  220.       case -1:
  221.     eprintf(failed_memory, (LittleLong(Header.numentries) + 1) * sizeof(struct wadentry), "wadentries");
  222.     break;
  223.       case 0:
  224.     if ((entryName) && (*entryName != '\0') && (outType != TYPE_WAD2)) {
  225.       eprintf("no entry %s found in wad\n", entryName);
  226.       break;
  227.     }
  228.       default:{
  229.       struct rgb *Palette;
  230.       int i, parseCount;
  231.  
  232.       parseCount = LittleLong(Header.numentries);
  233.       Palette = 0;
  234.  
  235.       if ((outType != TYPE_NONE) &&
  236.           ((procOper == OP_VIEW) ||
  237.            (procOper == OP_EXTRACT))) {
  238.         /*
  239.          * first search for a palette in the wad-file and use that instead
  240.          * of default palette 
  241.          */
  242.         struct wadentry *palEntry = allEntries;
  243.  
  244.         for (i = 0; i < parseCount; i++) {
  245.           if (palEntry->type == WAD2_PALETTE) {
  246.         __lseek(wadFile, LittleLong(palEntry->offset), SEEK_SET);
  247.         switch (palEntry->compr) {
  248.           case CMP_NONE:
  249.           case CMP_MIP0:
  250.             if ((Palette = (struct rgb *)tmalloc(256 * 3)))
  251.               __read(wadFile, Palette, 256 * 3);
  252.             break;
  253.           case CMP_LZ77:
  254.           case (CMP_LZ77 | CMP_MIP0):
  255.             Palette = (struct rgb *)GetLZ77(wadFile, LittleLong(palEntry->wadsize));
  256.             break;
  257.           default:
  258.             break;
  259.         }
  260.         mprintf("use wads built-in palette\n");
  261.         break;
  262.           }
  263.           palEntry++;
  264.         }
  265.  
  266.         /* we take the new palette for conversions */
  267.         if (!Palette)
  268.           Palette = GetPalette();
  269.         else
  270.           cachedPalette = Palette;
  271.       }
  272.  
  273.       /* process only ONE */
  274.       if (Entry && (outType != TYPE_WAD2)) {
  275.         i = Entry - allEntries;
  276.         parseCount = i + 1;
  277.       }
  278.       /* reset and process ALL */
  279.       else {
  280.         i = 0;
  281.         Entry = allEntries;
  282.       }
  283.  
  284.       for (; i < parseCount; i++, Entry++) {
  285.         char fileName[NAMELEN_PATH];
  286.  
  287.         __strncpy(fileName, destDir, NAMELEN_PATH - 1);
  288.         __strncat(fileName, Entry->name, NAMELEN_PATH - 1);
  289.  
  290.         if (outType == TYPE_NONE) {
  291.           AppendType(fileName, Entry->type, ".xxx");
  292.           switch (Entry->type) {
  293.         case WAD2_STATUSBAR:
  294.         case WAD2_CONPIC:    outType = TYPE_LUMP;   break;
  295.         case WAD2_MIPMAP:    outType = TYPE_MIPMAP; break;
  296.         case WAD2_PALETTE:
  297.         default:        outType = TYPE_NONE;   break;
  298.           }
  299.         }
  300.         else
  301.           AppendType(fileName, outType, ".xxx");
  302.  
  303.         switch (procOper) {
  304.           case OP_EXTRACT:
  305.         if (outType == TYPE_WAD2) {
  306.           struct rawdata *rawData;
  307.  
  308.           mprintf(oper_extract, Entry->name, entryName);
  309.           if ((rawData = GetWAD2Raw(wadFile, Entry))) {
  310.             retval = AddWAD2(0, rawData, entryName, OP_ADD, Entry->type);
  311.             rfree(rawData);
  312.           }
  313.         }
  314.         else {
  315.           FILE *fileDst;
  316.  
  317.           mprintf(oper_extract, Entry->name, fileName);
  318.           CreatePath(fileName);
  319.           if ((fileDst = __fopen(fileName, F_WRITE_BINARY))) {
  320.             if (outType == TYPE_NONE) {
  321.               struct rawdata *rawData;
  322.  
  323.               if ((rawData = GetWAD2Raw(wadFile, Entry))) {
  324.             retval = PutRaw(fileno(fileDst), rawData);
  325.             rfree(rawData);
  326.               }
  327.             }
  328.             else {
  329.               struct palpic *Picture;
  330.  
  331.               if ((Picture = GetWAD2Picture(wadFile, Entry))) {
  332.             switch (outType) {
  333.               case TYPE_LUMP:   retval = PutLMP(fileno(fileDst), Picture);    break;
  334.               case TYPE_MIPMAP: retval = PutMipMap(fileno(fileDst), Picture); break;
  335.               default:        retval = PutImage(fileDst, Picture, outType); break;
  336.             }
  337.             pfree(Picture);
  338.               }
  339.             }
  340.             __fclose(fileDst);
  341.           }
  342.           else
  343.             eprintf(failed_fileopen, fileName);
  344.         }
  345.         break;
  346.           case OP_VIEW:{
  347.           struct palpic *Picture;
  348.  
  349.           mprintf(oper_view, Entry->name, fileName);
  350.           if ((Picture = GetWAD2Picture(wadFile, Entry))) {
  351.             if (DisplayPicture(Picture->rawdata, Picture->name, Picture->width, Picture->height, 8, TRUE))
  352.               i = parseCount;
  353.             pfree(Picture);
  354.             retval = TRUE;
  355.           }
  356.         }
  357.         break;
  358.           case OP_DELETE:{
  359.           int diff, last;
  360.  
  361.           diff = LittleLong(Entry->wadsize);
  362.           last = LittleLong(Entry->offset);
  363.  
  364.           mprintf(oper_delete, Entry->name, fileName);
  365.           /* delete from disk */
  366.           __lseek(wadFile, last, SEEK_SET);
  367.           if (CutOff(wadFile, diff, 0)) {
  368.             int content;
  369.  
  370.             /* correct header */
  371.             content = LittleLong(Header.numentries) - 1;
  372.             Header.numentries = LittleLong(content);
  373.             Header.offset = LittleLong(LittleLong(Header.offset) - diff);
  374.  
  375.             /* delete from memlist */
  376.             __memcpy(Entry, Entry + 1, (content - i) * sizeof(struct wadentry));
  377.             /* delete from loop */
  378.             parseCount--;
  379.             i--;
  380.             Entry--;
  381.  
  382.             /* correct all following offsets */
  383.             while (content--)
  384.               /* correct all, that come afterwards */
  385.               if (LittleLong(allEntries[content].offset) >= last)
  386.             allEntries[content].offset = LittleLong(LittleLong(Entry->offset) - diff);
  387.  
  388.             /* dump header */
  389.             __lseek(wadFile, 0, SEEK_SET);
  390.             __write(wadFile, &Header, sizeof(struct wadheader));
  391.  
  392.             /* dump entries */
  393.             __lseek(wadFile, LittleLong(Header.offset), SEEK_SET);
  394.             __write(wadFile, allEntries, LittleLong(Header.numentries) * sizeof(struct wadentry));
  395.  
  396.             /* cut off removed entry */
  397.             retval = CutOff(wadFile, sizeof(struct wadentry), 0);
  398.           }
  399.         }
  400.         break;
  401.           case OP_LIST:
  402.           case OP_DEFAULT:
  403.           default:
  404.         mprintf("%16s %8d (%8d bytes, offset: %8x, type: %c, compr: %1d)\n", Entry->name, LittleLong(Entry->wadsize), LittleLong(Entry->memsize), LittleLong(Entry->offset), Entry->type, (int)Entry->compr);
  405.         retval = TRUE;
  406.         break;
  407.         }
  408.  
  409.         if (script)
  410.           fprintf(script, "update %s as %s as %c\n", fileName, Entry->name, Entry->type);
  411.       }
  412.     }
  413.     break;
  414.     }
  415.     tfree(allEntries);
  416.   }
  417.   else
  418.     eprintf("no valid wadfile\n");
  419.  
  420.   cachedPalette = oldCache;
  421.   return retval;
  422. }
  423.  
  424. /*
  425.  * inputs:
  426.  *  inPic  - a picture that should be converted to the wadtype, that is a high-level-call
  427.  *  inData - some data, we dont know what it is, and what wadType it should be
  428.  *  Entry  - the entry we associate the data to, type must be set before, wadsize and mem-
  429.  *           size will be set by the routine
  430.  *
  431.  * in compression-case the size of rawData is wrong, ever expect the entry-sizes are the
  432.  * right ones, never the rawdata-sizes
  433.  */
  434. struct rawdata *PutWAD2Raw(struct palpic *inPic, struct rawdata *inData, struct wadentry *Entry)
  435. {
  436.   struct rawdata *rawData = 0;
  437.   int newsize;
  438.   
  439.   if (inData) {
  440.     switch (Compression & CMP_LZ77) {
  441.       case CMP_NONE:
  442.         rawData = inData;
  443.         newsize = rawData->size;
  444.     break;
  445.       case CMP_LZ77:
  446.     if((rawData = rmalloc(inData->size * 2, inData->name)))
  447.       if((newsize = PasteLZ77(rawData->rawdata, (char *)inData->rawdata, inData->size)) <= ERROR) {
  448.         rfree(rawData);
  449.         rawData = 0;
  450.       }
  451.     break;
  452.     }
  453.  
  454.     if(rawData) {
  455.       Entry->memsize = LittleLong(rawData->size);
  456.       Entry->wadsize = LittleLong(newsize);
  457.     }
  458.   }
  459.   else if (inPic) {
  460.     /*
  461.      * convert all the different types to rawData and then recall
  462.      * PutWADRaw itself to handle compression after all
  463.      */
  464.     switch (Entry->type) {
  465.       case WAD2_PALETTE:
  466.     if((rawData = rmalloc(256 * 3, inPic->name)))
  467.       __memcpy(rawData->rawdata, inPic->palette, 256 * 3);
  468.     break;
  469.       case WAD2_MIPMAP:
  470.     switch (Compression & CMP_MIP0) {
  471.       case CMP_NONE:
  472.         if((rawData = rmalloc(MIP_MULT(inPic->width * inPic->height) + sizeof(struct mipmap), inPic->name)))
  473.           if(!PasteMipMap((struct mipmap *)rawData->rawdata, inPic)) {
  474.             rfree(rawData);
  475.             rawData = 0;
  476.           }
  477.         break;
  478.       case CMP_MIP0:
  479.         if((rawData = rmalloc((inPic->width * inPic->height) + sizeof(struct mipmap), inPic->name)))
  480.           if(!PasteMipMap0((struct mipmap *)rawData->rawdata, inPic)) {
  481.             rfree(rawData);
  482.             rawData = 0;
  483.           }
  484.         break;
  485.     }
  486.     break;
  487.       case WAD2_STATUSBAR:
  488.       case WAD2_CONPIC:
  489.     if((rawData = rmalloc((inPic->width * inPic->height) + sizeof(struct lump), inPic->name)))
  490.       if(!PasteLMP((struct lump *)rawData->rawdata, inPic)) {
  491.         rfree(rawData);
  492.         rawData = 0;
  493.       }
  494.     break;
  495.       default:
  496.     eprintf("you must specify the wad-entry-type\n");
  497.     break;
  498.     }
  499.  
  500.     if(rawData) {
  501.       if (Compression & CMP_LZ77) {
  502.         struct rawdata *cmpData;
  503.     
  504.         cmpData = PutWAD2Raw(0, rawData, Entry);
  505.         rfree(rawData);
  506.         rawData = cmpData;
  507.       }
  508.       else
  509.         Entry->memsize = Entry->wadsize = LittleLong(rawData->size);
  510.     }
  511.   }
  512.   else
  513.     eprintf("nothing to add\n");
  514.  
  515.   Entry->compr = Compression;
  516.   return rawData;
  517. }
  518.  
  519. bool AddWAD2(struct palpic *inPic, struct rawdata *inData, char *wadName, operation procOper, filetype wadType)
  520. {
  521.   char *procName;
  522.   HANDLE wadFile = __open(wadName, H_READWRITE_BINARY_OLD);
  523.   struct wadheader Header;
  524.   struct wadentry *Entry, *allEntries;
  525.   bool retval = FALSE;
  526.   struct rawdata *writeData;
  527.  
  528.   if (wadFile < 0)
  529.     wadFile = __open(wadName, H_READWRITE_BINARY_NEW);
  530.  
  531.   if (wadFile > 0) {
  532.     if (CheckWAD2(wadFile, &Header, TRUE)) {
  533.       if (inPic) {
  534.     procName = inPic->name;
  535.       }
  536.       else if (inData) {
  537.     procName = inData->name;
  538.       }
  539.       /*
  540.        * we assume that the dir is at the end of the file!!!!! 
  541.        */
  542.       switch ((long int)(Entry = FindWAD2(wadFile, procName, &Header, &allEntries, wadType))) {
  543.     case -1:
  544.       eprintf(failed_memory, LittleLong(Header.numentries) * sizeof(struct wadentry), "wadentries");
  545.  
  546.       break;
  547.     case 0:
  548.       switch (procOper) {
  549.         case OP_REPLACE:
  550.           eprintf("no entry %s found to replace in wad %s\n", procName, wadName);
  551.           break;
  552.         case OP_UPDATE:
  553.           mprintf(oper_update, Entry->name[0] ? Entry->name : procName, wadName);
  554.           goto skip3;
  555.         case OP_ADD:
  556.         case OP_DEFAULT:
  557.         default:
  558.           mprintf(oper_add, Entry->name[0] ? Entry->name : procName, wadName);
  559.         skip3:
  560.           __lseek(wadFile, LittleLong(Header.offset), SEEK_SET);
  561.           /*
  562.            * seek to end of data and write data
  563.            */
  564.           Entry = allEntries + LittleLong(Header.numentries);
  565.           Entry->offset = Header.offset;
  566.           Entry->type = wadType;
  567.  
  568. #if 0
  569.           if((writeData = PutWADRaw(inPic, inData, Entry))) {
  570.         /* dump data */
  571.             __write(wadfile, writeData->rawdata, LittleLong(Entry->wadsize));
  572. #else
  573.           if (inData) {
  574.         switch (Compression) {
  575.           case CMP_NONE:
  576.           case CMP_MIP0:
  577.             retval = PutRaw(wadFile, inData);
  578.             break;
  579.           case CMP_LZ77:
  580.           case (CMP_MIP0 | CMP_LZ77):{
  581.               retval = PutLZ77(wadFile, (char *)inData->rawdata, inData->size) > ERROR ? TRUE : FALSE;
  582.               Entry->memsize = LittleLong(inData->size);
  583.             }
  584.             break;
  585.           default:
  586.             break;
  587.         }
  588.           }
  589.           else if (inPic) {
  590.         switch (wadType) {
  591.           case WAD2_PALETTE:
  592.             switch (Compression) {
  593.               case CMP_NONE:
  594.               case CMP_MIP0:
  595.             if (__write(wadFile, inPic->palette, 256 * 3) == (256 * 3)) {
  596.               retval = TRUE;
  597.             }
  598.             break;
  599.               case CMP_LZ77:
  600.               case (CMP_MIP0 | CMP_LZ77):{
  601.               retval = PutLZ77(wadFile, (char *)inPic->palette, (256 * 3)) > ERROR ? TRUE : FALSE;
  602.               Entry->memsize = LittleLong(256 * 3);
  603.             }
  604.             break;
  605.               default:
  606.             break;
  607.             }
  608.             break;
  609.           case WAD2_MIPMAP:
  610.             switch (Compression) {
  611.               case CMP_NONE:
  612.             retval = PutMipMap(wadFile, inPic);
  613.             break;
  614.               case CMP_MIP0:
  615.             retval = PutMipMap0(wadFile, inPic);
  616.             break;
  617.               case CMP_LZ77:{
  618.               struct mipmap *MipMap;
  619.  
  620.               if ((MipMap = (struct mipmap *)tmalloc(MIP_MULT(inPic->width * inPic->height) + sizeof(struct mipmap)))) {
  621.                 if ((retval = PasteMipMap(MipMap, inPic))) {
  622.                   retval = PutLZ77(wadFile, (char *)MipMap, MIP_MULT(inPic->width * inPic->height) + sizeof(struct mipmap)) > ERROR ? TRUE : FALSE;
  623.                   Entry->memsize = LittleLong(MIP_MULT(inPic->width * inPic->height) + sizeof(struct mipmap));
  624.                 }
  625.                 tfree(MipMap);
  626.               }
  627.             }
  628.             break;
  629.               case (CMP_MIP0 | CMP_LZ77):{
  630.               struct mipmap *MipMap;
  631.  
  632.               if ((MipMap = (struct mipmap *)tmalloc((inPic->width * inPic->height) + sizeof(struct mipmap)))) {
  633.                 if ((retval = PasteMipMap0(MipMap, inPic))) {
  634.                   retval = PutLZ77(wadFile, (char *)MipMap, (inPic->width * inPic->height) + sizeof(struct mipmap)) > ERROR ? TRUE : FALSE;
  635.                   Entry->memsize = LittleLong((inPic->width * inPic->height) + sizeof(struct mipmap));
  636.                 }
  637.                 tfree(MipMap);
  638.               }
  639.             }
  640.             break;
  641.               default:
  642.             break;
  643.             }
  644.             break;
  645.           case WAD2_STATUSBAR:
  646.           case WAD2_CONPIC:
  647.             switch (Compression) {
  648.               case CMP_NONE:
  649.               case CMP_MIP0:
  650.             retval = PutLMP(wadFile, inPic);
  651.             break;
  652.               case CMP_LZ77:
  653.               case (CMP_MIP0 | CMP_LZ77):{
  654.               struct lump *Lump;
  655.  
  656.               if ((Lump = (struct lump *)tmalloc((inPic->width * inPic->height) + sizeof(struct lump)))) {
  657.                 if ((retval = PasteLMP(Lump, inPic))) {
  658.                   retval = PutLZ77(wadFile, (char *)Lump, (inPic->width * inPic->height) + sizeof(struct lump)) > ERROR ? TRUE : FALSE;
  659.                   Entry->memsize = LittleLong(MIP_MULT(inPic->width * inPic->height) + sizeof(struct lump));
  660.                 }
  661.                 tfree(Lump);
  662.               }
  663.             }
  664.             break;
  665.               default:
  666.             break;
  667.             }
  668.             break;
  669.           default:
  670.             eprintf("you must specify the wad-entry-type\n");
  671.             break;
  672.         }
  673.           }
  674.           else
  675.         eprintf("nothing to add\n");
  676.  
  677.           if (retval) {
  678.         Entry->wadsize = LittleLong(__ltell(wadFile) - LittleLong(Header.offset));
  679.         if ((Entry->compr = Compression) == CMP_NONE)
  680.           Entry->memsize = Entry->wadsize;
  681. #endif
  682.         __strncpy(Entry->name, procName, NAMELEN_WAD);
  683.         Header.numentries = LittleLong(LittleLong(Header.numentries) + 1);
  684.         Header.offset = LittleLong(__ltell(wadFile));
  685.         /* write directory */
  686.         if (__write(wadFile, allEntries, LittleLong(Header.numentries) * sizeof(struct wadentry)) == (LittleLong(Header.numentries) * sizeof(struct wadentry))) {
  687.           /* write header */
  688.           __lseek(wadFile, 0, SEEK_SET);
  689.           __write(wadFile, &Header, sizeof(struct wadheader));
  690.         }
  691.         else {
  692.           eprintf(failed_filewrite, wadName);
  693.           retval = FALSE;
  694.         }
  695.           }
  696.           else
  697.         eprintf("cannot write data %c %s to wad %s\n", wadType, procName, wadName);
  698.           break;
  699.       }
  700.       tfree(allEntries);
  701.       break;
  702.     default:
  703.       switch (procOper) {
  704.         case OP_REPLACE:
  705.           mprintf(oper_replace, Entry->name[0] ? Entry->name : procName, wadName);
  706.           goto skip4;
  707.         case OP_UPDATE:
  708.           mprintf(oper_update, Entry->name[0] ? Entry->name : procName, wadName);
  709.         skip4:{
  710. #if 0
  711.         int diff, last;
  712.  
  713.         diff = LittleLong(Entry->wadsize);
  714.         last = LittleLong(Entry->offset);
  715.  
  716.         if((writeData = PutWAD2Raw(inPic, inData, Entry))) {
  717.           diff -= LittleLong(Entry->wadsize);
  718.  
  719.           __lseek(wadFile, last, SEEK_SET);
  720.           if (diff > 0) {
  721.             if (CutOff(wadFile, diff, 0))
  722.               retval = TRUE;
  723.           }
  724.           else if (diff < 0) {
  725.             if (PasteIn(wadFile, -diff, 0))
  726.               retval = TRUE;
  727.           }
  728.  
  729.           if (retval) {
  730.             int content;
  731.  
  732.             /* dump data */
  733.             __lseek(wadFile, last, SEEK_SET);
  734.             __write(wadfile, writeData->rawdata, LittleLong(Entry->wadsize));
  735.  
  736.             /* correct header */
  737.             content = LittleLong(Header.numentries);
  738.             Header.offset = LittleLong(LittleLong(Header.offset) - diff);
  739.  
  740.             /* correct all following offsets */
  741.             while (content--)
  742.               /* correct all, that come afterwards */
  743.               if (LittleLong(allEntries[content].offset) >= last)
  744.                 allEntries[content].offset = LittleLong(LittleLong(Entry->offset) - diff);
  745.  
  746.             /* correct entry */
  747.             Entry->offset = LittleLong(last);
  748.  
  749.             /* dump header */
  750.             __lseek(wadFile, 0, SEEK_SET);
  751.             __write(wadFile, &Header, sizeof(struct packheader));
  752.  
  753.             /* dump entries */
  754.             __lseek(wadFile, LittleLong(Header.offset), SEEK_SET);
  755.             __write(wadFile, &allEntries, LittleLong(Header.size));
  756.           }
  757.         }
  758. #endif
  759.           }
  760.           break;
  761.         case OP_ADD:
  762.         case OP_DEFAULT:
  763.         default:
  764.           eprintf("old entry %s found in wad %s\n", procName, wadName);
  765.           break;
  766.       }
  767.       tfree(allEntries);
  768.       break;
  769.       }
  770.       __close(wadFile);
  771.     }
  772.     else
  773.       eprintf("no valid wadfile %s\n", wadName);
  774.   }
  775.   else
  776.     eprintf(failed_fileopen, wadName);
  777.  
  778.   return retval;
  779. }
  780.